package admin

import (
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
	"anban/models"
	"anban/service"
	"anban/utils"
)

type WechatReplyController struct {
	BaseController
}

func (c *WechatReplyController) Add() {
	c.Data["replyTypeList"] = models.WechatReplyType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/wechat_reply/add.html"
}

func (c *WechatReplyController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	mType, _ := strconv.Atoi(input["type"][0])
	if mType <= 0 {
		res = utils.ResJson(0, "请选择回复类型", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["keyword"][0]) <= 0 {
		res = utils.ResJson(0, "请输入关键字", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if mType == 1 {
		if len(input["text"][0]) <= 0 {
			res = utils.ResJson(0, "请输入回复文本内容", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if mType == 2 {
		if len(input["media_id"][0]) <= 0 {
			res = utils.ResJson(0, "请上传回复图片", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	_, err := service.AddWechatReply(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatReplyController) Edit() {
	id := utils.Atoi64(c.Ctx.Input.Param(":id"))
	p := map[string]interface{}{}
	p["id"] = id
	record := service.GetWechatReplyInfo(p)
	c.Data["record"] = record
	c.Data["replyTypeList"] = models.WechatReplyType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/wechat_reply/edit.html"
}

func (c *WechatReplyController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	mType, _ := strconv.Atoi(input["type"][0])
	if mType <= 0 {
		res = utils.ResJson(0, "请选择回复类型", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["keyword"][0]) <= 0 {
		res = utils.ResJson(0, "请输入关键字", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if mType == 1 {
		if len(input["text"][0]) <= 0 {
			res = utils.ResJson(0, "请输入回复文本内容", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if mType == 2 {
		if len(input["media_id"][0]) <= 0 {
			res = utils.ResJson(0, "请上传回复图片", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	_, err := service.EditWechatReply(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatReplyController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	keyword := c.GetString("keyword")
	c.Data["keyword"] = keyword
	if len(keyword) > 0 {
		p["keyword"] = keyword
	}
	p["convert"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetWechatReplyList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/wechat_reply/list.html"
}